#ifndef _DIALOGSOCKET_HPP_
#define _DIALOGSOCKET_HPP_

#include "jhdefs.hpp"
#include "net/Socket.hpp"
#include "net/SocketAddress.hpp"
#include "net/StreamSocket.hpp"
#include <string>

NET_NAMESPACE_BEGIN

class CDialogSocket : public CStreamSocket
{
public:
	CDialogSocket();
	explicit CDialogSocket(const CSocketAddress& address);
	CDialogSocket(const CSocket& socket);
	CDialogSocket(const CDialogSocket& socket);
	~CDialogSocket();
	CDialogSocket& operator = (const CSocket& socket);
	CDialogSocket& operator = (const CDialogSocket& socket);

	void sendByte(unsigned char ch);
	void sendString(const char* str);
	void sendString(const std::string& str);

	void sendMessage(const std::string& message);
	void sendMessage(const std::string& message, const std::string& arg);
	void sendMessage(const std::string& message, const std::string& arg1, const std::string& arg2);

	bool receiveMessage(std::string& message);
	int receiveStatusMessage(std::string& message);

	int get();
	int peek();
	int receiveRawBytes(void* buffer, int length);
	void synch();

	void sendTelnetCommand(unsigned char command);
	void sendTelnetCommand(unsigned char command, unsigned char arg);

	enum TelnetCodes
	{
		TELNET_SE   = 240,
		TELNET_NOP  = 241,
		TELNET_DM   = 242,
		TELNET_BRK  = 243,
		TELNET_IP   = 244,
		TELNET_AO   = 245,
		TELNET_AYT  = 246,
		TELNET_EC   = 247,
		TELNET_EL   = 248,
		TELNET_GA   = 249,
		TELNET_SB   = 250,
		TELNET_WILL = 251,
		TELNET_WONT = 252,
		TELNET_DO   = 253,
		TELNET_DONT = 254,
		TELNET_IAC  = 255
	};

protected:
	void allocBuffer();
	void refill();
	bool receiveLine(std::string& line);
	int receiveStatusLine(std::string& line);

private:
	enum
	{
		RECEIVE_BUFFER_SIZE = 1024,
		EOF_CHAR            = -1
	};

	char* m_pBuffer;
	char* m_pNext;
	char* m_pEnd;
};

NET_NAMESPACE_END

#endif
